import _ from 'lodash'
export const useState = (init) => {
	const getInitState = () => unref(init);
	const state = ref(getInitState());
	const setState = (patch, cover = false) => {
		const newState = unref(patch);
		if (cover) state.value = newState;
		else
			state.value =
			newState != null || undefined ?
			typeof newState == 'object' ?
			_.merge(state.value, newState) :
			(state.value = newState) :
			state.value;
	};
	const getState = () => state.value;
	return [readonly(state), setState, getState];
};
import { dictStore } from 'store/dicts.js';
import { usersStore } from 'store/users.js';
import Api from 'api'

export const useDetails = (init,url)=>{
	const store1 = dictStore();
	const store = usersStore();
	const api = new Api
	onShow(() => {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 0
		});
		let pages = getCurrentPages();
		let currentPage = pages[pages.length - 1];
		init(currentPage)
	});
	const fanhui = () => {
		uni.navigateBack();
	};
	const edit = (id) => {
		uni.navigateTo({
			url: '/' + url +'/add/add?id=' + id
		});
	};
	const setTittle = (a,b,isShangbao)=>{
		uni.setNavigationBarTitle({
			title: (store.isFuJing() && isShangbao)?a:b
		});
	}
	return {
		store1,store,setTittle,
		api, fanhui, edit
	}
}
export const useGetData = (apiUrl, path,zidian) => {
	const store1 = dictStore();
	const api = new Api
	onShow(async() => {
		if(zidian){
			const res = await store1.getDict(zidian);
			ziDianList.value = res
		}
		getList();
	});
	const isTriggered = ref(false)
	const show = ref(false);
	const deleteShow = ref(false);
	const isDelete = ref(false);
	const success = ref('');
	const listData = ref([]);
	const [operateItem, setOperateItem] = useState({});
	const [total, setTotal] = useState(0);
	const ziDianList = ref([])
	const getList = async () => {
		const {
			records,
			total
		} = await api.list(apiUrl,screenData);
		listData.value = records;
		isTriggered.value = false;
		setTotal(total);
	};
	const screenData = reactive({
		status: '0',
		keywords: '',
		pageNo: 1,
		pageSize: 10
	});
	const add = () => {
		uni.navigateTo({
			url: `/${path}/add/add`
		});
	};
	const scrolltolower = () => {
		console.log('上拉了');
		if (unref(total) <= listData.value.length) return;
		screenData.pageSize += 10;
		getList();
	};
	const refresherrefresh = () => {
		console.log('下拉了');
		isTriggered.value = true;
		screenData.pageSize = 10;
		getList();
	};
	const clickItem = (id) => {
		uni.navigateTo({
			url: `/${path}/details/details?id=` + id
		});
	};
	const changeScreenData = () => {
		screenData.pageSize = 10;
		getList();
	};
	const operate = (item) => {
		setOperateItem(item);
		show.value = true;
	};
	const deleteItem = () => {
		deleteShow.value = true;
		show.value = false;
	};
	const confirm = async () => {
		deleteShow.value = false;
		const id = unref(operateItem).id;
		const res = await api.deleteData(apiUrl,{
			id
		});
		success.value = res;
		isDelete.value = true;
	};
	const fanhui = () => {
		isDelete.value = false;
		getList();
	};
	const editItem = () => {
		show.value = false;
		const id = unref(operateItem).id;
		uni.navigateTo({
			url: `/${path}/add/add?id=` + id
		});
		deleteShow.value = false;
	};
	return {
		total,ziDianList,
		setTotal,
		screenData,
		isTriggered,
		listData,
		changeScreenData,
		scrolltolower,
		refresherrefresh,
		clickItem,
		operateItem,
		setOperateItem,
		operate,
		show,
		deleteItem,
		deleteShow,
		success,
		isDelete,
		confirm,
		fanhui,
		editItem,
		add
	}
}